package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemServiceHelper;
import id.ac.amikom.libs.CustomAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AdemUserGroupActivity extends ListActivity {
	
	private ListAdapter adapter;
	private JSONArray jsonArray;
	private String[] id;
	private String[] jml;
	private String[] group;
	
	private class GetDataTask extends AsyncTask<Void, Void, Boolean> {

		ProgressDialog dialog = new ProgressDialog(
				AdemUserGroupActivity.this);
		boolean result = false;

		@Override
		protected Boolean doInBackground(Void... params) {
			AdemServiceHelper srv = new AdemServiceHelper(AdemUserGroupActivity.this);
			jsonArray = srv.getUserStats();
			result = true;
			
			return result;

		}

		protected void onPreExecute() {
			this.dialog.setMessage("Loading Users..");
			this.dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {			
								
			if(result)
				setList();				
			else{
								
				Toast.makeText(AdemUserGroupActivity.this, "No Internet Connection",
						Toast.LENGTH_LONG).show();
				
				finish();
			}
			
			this.dialog.dismiss();	
		}

	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.adem_title_list);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.adem_title);
		
		new GetDataTask().execute();				
		

	}
	
	public void setList() {
		try {

			ArrayList<HashMap<String, Object>> newsList = new ArrayList<HashMap<String, Object>>();
			
			id = new String[jsonArray.length()];
			jml = new String[jsonArray.length()];
			group = new String[jsonArray.length()];		
			
			for (int i = 0; i < jsonArray.length(); i++) {
								 								
				JSONObject json = jsonArray.getJSONObject(i);
				HashMap<String, Object> map = new HashMap<String, Object>();
				
				id[i] = json.getString("id").toString().trim();
				jml[i] = json.getString("jml").toString().trim();
				group[i] = json.getString("group").toString().trim();								
								
				if(Integer.parseInt(id[i])==1) map.put("icon", R.drawable.group1);
				else if(Integer.parseInt(id[i])==2) map.put("icon", R.drawable.group2);
				else if(Integer.parseInt(id[i])==3) map.put("icon", R.drawable.group3);
								
				map.put("group",json.getString("group"));
				map.put("jml", "Active Users. " + json.getString("jml"));
				newsList.add(map);

			}

			adapter = new CustomAdapter(this, newsList,
					R.layout.adem_list_img, new String[] { "group", "jml", "icon" },
					new int[] { R.id.list_title, R.id.list_date, R.id.list_image });

			setListAdapter(adapter);
			registerForContextMenu(getListView());

		} catch (JSONException e) {
			e.printStackTrace();

		}
	}
	
	public void onListItemClick(ListView l, View v, int position, long id){
		super.onListItemClick(l, v, position, id);
		
		String gid = this.id[position];
		Intent i = new Intent(AdemUserGroupActivity.this, AdemListUserActivity.class);       
        i.putExtra("_id", gid);
        
        startActivity(i);
		
		
    }
	
}
